let gulp     = require("gulp"); 
let connect  = require("gulp-connect");
let proxy    = require('http-proxy-middleware')
var sass     = require('gulp-sass');
sass.compiler = require('node-sass');

let { paths , proxyList } = require("./config");

function devStyles(){
      return gulp.src(paths.styles.src)
            // .pipe(sass().on('error', sass.logError))
            .pipe(gulp.dest(paths.styles.dist))
            .pipe(connect.reload());
}
// function devFonts(){
//       return gulp.src(paths.font.src)
//             // .pipe(sass().on('error', sass.logError))
//             .pipe(gulp.dest(paths.font.dist))
//             .pipe(connect.reload());
// }
function devScripts(){
      return gulp.src(paths.scripts.src)
      .pipe(gulp.dest(paths.scripts.dist))
      .pipe(connect.reload());
}
// function devLibs(){
//       return gulp.src(paths.libs.src)
//       .pipe(gulp.dest(paths.libs.dist))
//       .pipe(connect.reload());
// }
function devHtml(){
      return gulp.src(paths.html.src)
      .pipe(gulp.dest(paths.html.dist))
      .pipe(connect.reload());
}
function devPhp(){
      return gulp.src(paths.php.src)
      .pipe(gulp.dest(paths.php.dist))
      .pipe(connect.reload());
}

function useconnect(){
      connect.server({
            root : "./src",
            port : 8888, 
            livereload : true,
            middleware : function(connect, opt) {
                  let arr = [];
                  for(let attr in proxyList){
                        arr.push( proxy ( attr , proxyList[attr]))
                  }
                  return arr;
            }
      });
}

function watch(){
      gulp.watch(paths.styles.src,devStyles)
      // gulp.watch(paths.font.src,devFonts)
      gulp.watch(paths.scripts.src,devScripts)
      // gulp.watch(paths.libs.src,devLibs)
      gulp.watch(paths.html.src,devHtml)
      gulp.watch(paths.php.src,devPhp)
}


let dev = gulp.series(gulp.parallel( useconnect , watch ));

module.exports  = dev; 
